package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.vo.SysAssetValue;
import com.ruoyi.system.domain.vo.SysAssetVo;
import com.ruoyi.system.mapper.SysAssetMapper;
import com.ruoyi.system.service.SysAssetDepreciationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Description:资产折旧service
 * Author: ZLZ
 * Date: 2025/4/2
 */
@Service
public class SysAssetDepreciationServiceImpl implements SysAssetDepreciationService {
    @Autowired
    private SysAssetMapper sysassetMapper;

    /**
     * 获取资产折旧值
     * @param assetId 资产id，必传
     * @param type 折旧方法（必传），1-年限平均法，2-双倍余额递减法，3-年数总和法
     * @param year 预计尚可使用年限，年数总和法必传
     * @return
     */
    @Override
    public SysAssetValue getAssetDepreciationValue(Long assetId, Integer type, Integer year) throws Exception {
        SysAssetValue sysAssetValue = new SysAssetValue();
        //获取资产预计使用年限和购买价值
        SysAssetVo sysAssetVo = sysassetMapper.selectSysAssetByAssetId(assetId);
        int useYear = 0;
        BigDecimal price;
        if(sysAssetVo != null && sysAssetVo.getUseYear() != null && sysAssetVo.getPurchasePrice() != null){
            useYear = sysAssetVo.getUseYear();
            price = sysAssetVo.getPurchasePrice();
        }else{
            throw new Exception("无该资产信息，或者该资产信息使用年限为null,或者该资产信息购买价格为null");
        }
        if(type == 1){ // 年平均法
            BigDecimal yearValue = new BigDecimal("0.95");
            yearValue = price.multiply(yearValue);
            yearValue = yearValue.divide(new BigDecimal(useYear), 2, RoundingMode.HALF_UP);
            sysAssetValue.setYearValue(yearValue);
            BigDecimal monValue = yearValue.divide(new BigDecimal(12), 2, RoundingMode.HALF_UP);
            sysAssetValue.setMonthValue(monValue);
        }else if(type == 2){
            BigDecimal yearValue = new BigDecimal(2);
            yearValue = yearValue.divide(new BigDecimal(useYear), 2, RoundingMode.HALF_UP);
            yearValue = price.multiply(yearValue);
            sysAssetValue.setYearValue(yearValue);
            BigDecimal monValue = yearValue.divide(new BigDecimal(12), 2, RoundingMode.HALF_UP);
            sysAssetValue.setMonthValue(monValue);
        }else{
            if(year == null){
                throw new Exception("年数总和法，预计尚可使用年限不能为空");
            }else{
                int sum = 0;
                for(int i = 1; i <= useYear; i++){
                    sum += i;
                }
                BigDecimal yearValue = new BigDecimal(year);
                yearValue = yearValue.divide(new BigDecimal(sum), 2, RoundingMode.HALF_UP);
                yearValue = yearValue.multiply(new BigDecimal("0.95").multiply(price)).setScale(2, RoundingMode.HALF_UP);
                sysAssetValue.setYearValue(yearValue);
                BigDecimal monValue = yearValue.divide(new BigDecimal(12), 2, RoundingMode.HALF_UP);
                sysAssetValue.setMonthValue(monValue);
            }
        }
        return sysAssetValue;
    }
}
